var express = require('express');
var router = express.Router();
const tableName = 'address'
const { Success, MError } = require("../utils/Result");
const Db = require("../utils/Db");


//添加收货地址数据 
router.post("/addressadd", async (req, res) => {
    let { uid, name, tel, province, city, county, addressDetail } = req.body;
    if (!uid || !name || !tel || !addressDetail) {
        res.send(MError("缺少必要字段")); return;
    } else {
        // 组装收获地址数据
        let insertData = { uid, name, tel: tel.toString(), province, city, county, addressDetail }
        // 添加收获地址数据
        result = await Db.insert(req, tableName, insertData);
        if (result) {
            res.send(Success({ addressid: result }, "地址添加成功"));
        } else {
            res.send(MError("添加失败，请查看字段信息是否正确"));
        }
    }

});

// 请求地址总数
router.get('/addresstotal', async (req, res) => {
    const output = {
        code: 200,
        msg: "操作成功",
    }
    const { uid } = req;
    const sql = `select count(id) as total from ${tableName} where uid = '${uid}'`;
    const result = await Db.select(req, sql);
    if (result) {
        output.list = result[0].total
    }
    res.send(output);
}),

    // 查询当前用户下的收货地址
    router.get('/addresslist', async (req, res) => {
        // 获取uid
        let { uid, page, size } = req.query;
        if (!uid || !page || !size) {
            res.send(MError("缺少必要条件"));
            return;
        } else {
            // 组装sql  根据用户的uid查询收获地址列表
            let sql = `SELECT * FROM  ${tableName} where uid = '${uid}' limit ${(page - 1) * size},${size}`;
            let addresslist = await Db.select(req, sql);
            // if (addresslist == null && !id) {// 兼容生成订单查询默认地址 如果没有默认地址就返回全部收货地址
            //     // 再次查询status == 0的数据
            //     addresslist = await Db.select(req, `SELECT * FROM  ${tableName} where uid = '${uid}' AND  status=0`);
            //     return res.send(Success(addresslist));
            // }
            if (!addresslist) {
                addresslist = [];
            }
            res.send(Success(addresslist));
        }

    })

//根据数据id修改一条收货地址
router.post("/addressedit", async (req, res) => {
    let { id, name, tel, province, city, county, addressDetail } = req.body;
    console.log(id, name, tel, province, city, county, addressDetail);
    if (!id || !name || !tel || !province || !city || !county || !addressDetail) {
        return res.send(MError("缺少必要字段"));
    } else {
        // 组装修改字段
        let updateData = { name, tel: tel.toString(), province, city, county, addressDetail }
        const result = await Db.update(req, tableName, updateData, ` WHERE id = ${id}`);
        result === true ? res.send(Success()) : res.send(MError(result));
    }
});

// 根据数据id删除一条收货地址
router.post("/addressremove", async (req, res) => {
    let { id } = req.body;
    if (!id) {
        res.send(MError("缺少必要条件")); return;
    } else {
        const result = await Db.delete(req, `DELETE FROM ${tableName} WHERE id = '${id}'`);
        if (result === true) {
            res.send(Success([], "删除成功"))
        } else {
            res.send(MError());
        }
    }
})


module.exports = router;